package cn.yuanqiao.web.controller.archive.Identify;

import java.io.InputStream;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.yuanqiao.archive.appManager.domain.TenantFields;
import cn.yuanqiao.archive.appManager.domain.TenantMdataList;
import cn.yuanqiao.common.utils.StringUtils;
import com.alibaba.fastjson2.JSONObject;
import lombok.Cleanup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import cn.yuanqiao.archive.Identify.domain.TenantMinganciku;
import cn.yuanqiao.archive.Identify.service.ITenantMingancikuService;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * minganciController
 *
 * @author 赵常飞
 * @date 2023-07-23
 */
@RestController
@RequestMapping("/archive/minganci")
public class TenantMingancikuController extends BaseController
{
    @Autowired
    private ITenantMingancikuService tenantMingancikuService;

    /**
     * 查询minganci列表
     */
    //@PreAuthorize("@ss.hasPermi('archive:minganci:list')")
    @Log(title = "查询列表", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(TenantMinganciku tenantMinganciku)
    {
        startPage();
        List<TenantMinganciku> list = tenantMingancikuService.selectTenantMingancikuList(tenantMinganciku);
        return getDataTable(list);
    }

    /**
     * 导出minganci列表
     */
    //@PreAuthorize("@ss.hasPermi('archive:minganci:export')")
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @PreAuthorize("@ss.hasPermi('archive:mgck:export')")
    public void export(HttpServletResponse response, @RequestBody JSONObject json)
    {
        List ids=json.getJSONArray("ids");
        List<TenantMinganciku> list = tenantMingancikuService.getexportlist(ids);
        ExcelUtil<TenantMinganciku> util = new ExcelUtil<TenantMinganciku>(TenantMinganciku.class);
        util.exportExcel(response, list, "敏感词库数据");
    }

    /**
     * 获取minganci详细信息
     */
    //@PreAuthorize("@ss.hasPermi('archive:minganci:query')")
    @Log(title = "获取详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tenantMingancikuService.selectTenantMingancikuById(id));
    }

    /**
     * 新增minganci
     */
    //@PreAuthorize("@ss.hasPermi('archive:minganci:add')")
    @Log(title = "添加", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('archive:mgck:add')")
    @PostMapping
    public AjaxResult add(@RequestBody TenantMinganciku tenantMinganciku)
    {
        return toAjax(tenantMingancikuService.insertTenantMinganciku(tenantMinganciku));
    }

    /**
     * 修改minganci
     */
    //@PreAuthorize("@ss.hasPermi('archive:minganci:edit')")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody TenantMinganciku tenantMinganciku)
    {
        return toAjax(tenantMingancikuService.updateTenantMinganciku(tenantMinganciku));
    }

    /**
     * 删除minganci
     */
    //@PreAuthorize("@ss.hasPermi('archive:minganci:remove')")
    @Log(title = "删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPermi('archive:mgck:delete')")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tenantMingancikuService.deleteTenantMingancikuByIds(ids));
    }
    @Log(title = "批量修改", businessType = BusinessType.UPDATE)
    @PostMapping("/plxg")
    @PreAuthorize("@ss.hasPermi('archive:mgck:editall')")
    public AjaxResult plxg(@RequestBody JSONObject json)
    {
        return toAjax(tenantMingancikuService.plxg(json));
    }

    @Log(title = "导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('archive:mgck:import')")
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        if(file.isEmpty()){
            return AjaxResult.error("参数错误！");
        }
        @Cleanup
        InputStream in=file.getInputStream();
        ExcelUtil<TenantMinganciku> util = new ExcelUtil<>(TenantMinganciku.class);
        List<TenantMinganciku> dataLists = util.importExcel(in);
        int j=tenantMingancikuService.importHddaMDataList(dataLists);
        if(j==dataLists.size()){
            return AjaxResult.error("敏感词已存在！");
        }
        return AjaxResult.success();
    }

    @Log(title = "下载导入模板", businessType = BusinessType.EXPORT)
    @PostMapping("/importmodel")
    public void importmodel(HttpServletResponse response) throws Exception
    {
        ExcelUtil<TenantMinganciku> util = new ExcelUtil<TenantMinganciku>(TenantMinganciku.class);
        util.importTemplateExcel(response, "基础数据");
    }
}
